package cn.antho.basic.listener;

import cn.antho.order.domain.OrderProduct;
import cn.antho.order.mapper.OrderProductMapper;
import cn.antho.product.domain.Product;
import cn.antho.product.domain.ProductOrderMsg;
import cn.antho.product.mapper.ProductMapper;
import cn.antho.product.mapper.ProductOrderMsgMapper;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "product_order_topic",selectorExpression="*",consumerGroup = "product_order_group")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {

    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private ProductOrderMsgMapper productOrderMsgMapper;


    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String msg = new String(body, CharsetUtil.UTF_8);
        log.info("接收到消息：{}", msg);
        OrderProduct orderProduct = orderProductMapper.findById(Long.valueOf(msg));
        if (orderProduct.getState() == 0) {
            orderProduct.setState(-1);
            orderProductMapper.update(orderProduct);
            ProductOrderMsg productOrderMsg = productOrderMsgMapper.findById(orderProduct.getProductOrderMsgId());
            productOrderMsg.setState(-1);
            productOrderMsgMapper.update(productOrderMsg);
        }

    }

}